﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Subscriber
    {
        #region Properties
        #endregion

        #region Methods
        #endregion

        #region Static Methods
        public static Subscriber Parse(System.Object value)
        {
            Subscriber temp = null;
            try
            {
                temp = (Subscriber)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<Subscriber> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Subscribers;
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Subscriber Subscriber)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (Subscriber.Email.Length < 0)
                errorList.Add(new ErrorItem("Email", "Email is required."));
            if (Subscriber.Email.Length > 100)
                errorList.Add(new ErrorItem("Email", "Email is only allow up to 100 characters."));
            if (Subscriber.Firstname.Length > 100)
                errorList.Add(new ErrorItem("Firstname", "Firstname is only allow up to 100 characters."));
            if (Subscriber.Lastname.Length > 100)
                errorList.Add(new ErrorItem("Lastname", "Lastname is only allow up to 100 characters."));
            if (Subscriber.Cellfone.Length > 20)
                errorList.Add(new ErrorItem("Cellfone", "Cellfone is only allow up to 20 characters."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Subscriber ii, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ii);
            if (errorList.Count == 0)
            {
                Nullable<Int32> id = null;
                int errorMess = _db.InsertSubscriber(id_User, ref id, ii.Email, ii.Firstname, ii.Lastname, ii.Cellfone, ii.Gender);
                if (errorMess == 1)
                    ii.ID = id.Value;
                
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Subscriber ic, int id_User, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Subscriber oi = _db.Subscribers.Single(p => p.ID == ic.ID);
                String content = "";
                content = oi.Email + "_" + oi.Firstname + "_" + oi.Lastname + "_" + oi.Cellfone + "_" + oi.Gender + "_" + ic.ID_Status;
                int errorMess = _db.ChangeSubscriber(id_User, ic.ID, ic.Email, ic.Firstname, ic.Lastname, ic.Cellfone, ic.Gender, ic.ID_Status, content, 0, 0);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Subscriber ic, int id_User, int id_Log, int id_Lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Subscriber oi = _db.Subscribers.Single(p => p.ID == ic.ID);
                int errorMess = _db.ChangeSubscriber(id_User, ic.ID, ic.Email, ic.Firstname, ic.Lastname, ic.Cellfone, ic.Gender, ic.ID_Status, "", id_Log, 1);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> Delete(int id_Subscriber, int id_User, int deep, int id_Lang)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.DeleteCustomer(id_User, id_Subscriber, deep);
            
                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));


            return errorList;
        }
        #endregion

        #region Search
        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID" || kvp.Key == "ID_Status")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "Gender")
                {
                    if (int.Parse(kvp.Value.ToString()) == 1 || int.Parse(kvp.Value.ToString()) == 0)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "Description" || kvp.Key == "Firstname" || kvp.Key == "Lastname" || kvp.Key == "Email" || kvp.Key == "Cellfone")
                {
                    if (exactList[kvp.Key])
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " = '" + kvp.Value.ToString() + "'";
                    }
                    else
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " LIKE '%" + kvp.Value.ToString() + "%'";
                    }
                }
            }

            return query;
        }

        public static System.Object GetSubscriber(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Subscriber>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion
    }
}